package com.hspedu.hspspringmvc.servlet;

import com.hspedu.hspspringmvc.annotation.Controller;
import com.hspedu.hspspringmvc.annotation.RequestMapping;
import com.hspedu.hspspringmvc.context.HspWebApplicationContext;
import com.hspedu.hspspringmvc.handler.HspHandler;
import com.hspedu.hspspringmvc.xml.XMLParser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//充当原生DispatcherServlet，本质是servlet
public class HspDispatcherServlet extends HttpServlet {
    private List<HspHandler> handlerList=new ArrayList<>();
     HspWebApplicationContext hspWebApplicationContext =null;
    @Override
    public void init() throws ServletException {
        hspWebApplicationContext= new HspWebApplicationContext();
        hspWebApplicationContext.init();
        initHandlerMapping();
        System.out.println("handlerList="+handlerList);
    }
    private void initHandlerMapping(){
        if (hspWebApplicationContext.ioc.isEmpty()){
            return;
        }
        for (Map.Entry<String,Object> entry:hspWebApplicationContext.ioc.entrySet()){
            Class<?> clazz = entry.getValue().getClass();
            if (clazz.isAnnotationPresent(Controller.class)){
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method method : declaredMethods) {
                    if(method.isAnnotationPresent(RequestMapping.class)){
                        //String value = method.getDeclaredAnnotation(RequestMapping.class).value();
                        //String contextPath = getServletContext().getContextPath();//获取工程路径
                        String url = method.getAnnotation(RequestMapping.class).value();
                        HspHandler hspHandler = new HspHandler(url,entry.getValue(),method);
                        handlerList.add(hspHandler);
                    }
                }
            }

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HspDispatcherServlet doGet被调用");
        doPost(req, resp);
        //super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HspDispatcherServlet doPost被调用");
        executeDispachter(req, resp);
        //super.doPost(req, resp);
    }
    private HspHandler getHspHandler(HttpServletRequest request){
        String requestURI = request.getRequestURI();
        for (HspHandler hspHandler : handlerList) {
            if (requestURI.equals(hspHandler.getUrl())){
                return hspHandler;
            }
        }
        return null;
    }

    private void executeDispachter(HttpServletRequest request,
                                  HttpServletResponse response){
        HspHandler hspHandler = getHspHandler(request);
        try {
            if (null== hspHandler){
                response.getWriter().write("<h1>404NOT FOUND</h1>");
            }else {
                Method method = hspHandler.getMethod();
                hspHandler.getMethod().invoke(hspHandler.getController(),request,response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
